/****************************
 * 常用基本－网络请求公共方法
 ****************************/

/**
 * GET请求共通方法
 **/
var qs = require('qs')

exports.SEND_HTTP = function (req, res, options, cb) {

	//最终请求参数
	var httpOptions = {};

	//判断是否有RESTFUL参数
	var restfulParams = "";
	if (!(common.isNullObj(options.params))) {
		restfulParams = common.JsonTransformRestFulParams(options.params);
	}

	//设置请求地址
	httpOptions.url = options.url + restfulParams;

	//设置请求方式
	httpOptions.method = options.method == undefined ? "GET" : options.method;

	//设置BODY参数
	httpOptions.form = options.data == undefined ? {} : qs.stringify(options.data);

	//设置超时时间
	httpOptions.timeout = config.networkTimeout;

	//设置压缩
	httpOptions.gzip = true;

	//设置返回值类型
	httpOptions.json = true;

	//获取传递的header头
	var headerObj = options.headers == undefined ? {} : options.headers;

	//获取聚合层服务器编号
	var serverNo = config.serverAuth.serverNo;

	//新增系统内置header头
	headerObj['server-No'] = serverNo;
	headerObj['Content-Type'] = 'application/x-www-form-urlencoded';

	//设置header头
	httpOptions.headers = headerObj;

	//发起HTTP请求
	request(httpOptions, function (error, response, body) {

	
		//判断是否请求有错误
		if (error) {

			//请求超时处理
			if (error.code == 'ESOCKETTIMEDOUT') {
				return RES.errorResponse(res, MESSAGE.apiError.noNetWorkTimeout);
			}

			return RES.errorResponse(res, MESSAGE.apiError.noNetWork);
		}

		//判断是否有业务错误
		if (!error && response.statusCode == 200) {

			//返回业务错误信息
			if (body.success) {
				return cb(body);
			} else {
				return RES.errorResponse(res, body.result.message || body.result);
			}

		}

	});

};


//通用http请求方法
exports.NetworkHTTP = function (req, res, options, cb) {

	//最终请求参数
	var httpOptions = {};

	//判断是否有RESTFUL参数
	var restfulParams = "";
	if (!(common.isNullObj(options.params))) {
		restfulParams = common.JsonTransformRestFulParams(options.params);
	}

	//设置请求地址
	httpOptions.url = options.url + restfulParams;

	//设置请求方式
	httpOptions.method = options.method == undefined ? "GET" : options.method;

	//设置BODY参数
	httpOptions.form = options.data == undefined ? {} : qs.stringify(options.data);

	//设置压缩
	httpOptions.gzip = true;

	//设置返回值类型
	httpOptions.json = true;

	//发起HTTP请求
	request(httpOptions, function (error, response, body) {

		//判断是否有业务错误
		if (!error && response.statusCode == 200) {

			cb(body);

		} else {

			return RES.errorResponse(res, error);

		}


	})
}


//SSH2执行docker命令
exports.Ssh_exec_dockerCommand = function (command, cb) {

	//获取ssh连接对象
	var SSHClient = require('ssh2').Client;

	//实例化连接对象
	var sshConnection = new SSHClient();

	var resultStr = "";

	var resultData = [];

	//监听连接状态
	sshConnection.on('ready', function () {

		sshConnection.exec('cd ' + config.dockerConfig.dockerShellDir + "\n./" + command, function (err, stream) {

			//监听结束
			stream.on('close', function (code, signal) {

				sshConnection.end();

				resultStr = resultData.toString();

				cb(resultStr)


			}).on('data', function (data) {

				resultData.push(data)

			}).stderr.on('data', function (data) {

				resultData.push(data)

			});

		})

	}).connect({

		host: config.sshConfig.ip,
		port: config.sshConfig.port,
		username: config.sshConfig.user,
		password: config.sshConfig.pass,

	})

}

